﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace DLL
{
    public class OutProductRepo
    {
        #region Paper
        public List<pOutProdPaper_Get_Result> GetOutProdPaper(DateTime? fromDate = null, DateTime? toDate = null)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                var historyPaper = dbContext.pOutProdPaper_Get(fromDate, toDate).ToList();
                return historyPaper;
            }
        }

        public void DeleteOutProdPaper(int id)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                dbContext.pOutProdPaper_Delete(id);
            }
        }

        public void AddOutPaper(OutProdPaper paper)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                dbContext.OutProdPapers.Add(paper);
                dbContext.SaveChanges();
            }
        }

        public void UpdateOutPaper(OutProdPaper paper)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                dbContext.pOutProdPaper_Update(paper.Id, paper.Date);
            }
        }

        public OutProdPaper GetOutProdPaperById(int id)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                return dbContext.OutProdPapers.Find(id);
            }
        }

        public int UpdateReferId(int outProdPaperId, int outProdDetailId, int inProdPaperId, int inProdDetaiId)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                var result = dbContext.pOutProd_UpdateReferId(outProdPaperId, outProdDetailId, inProdPaperId, inProdDetaiId);
                return result.FirstOrDefault().Value;
            }
        }
        #endregion Paper

        #region Detail
        public pOutProdDetail_Add_Result AddOutProdDetail(int amount, int productId, int outProdPaperId = 0, DateTime? date = null, int addBy = 0, int? toShopId = null)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                return dbContext.pOutProdDetail_Add(productId, amount, addBy, outProdPaperId, date, toShopId).FirstOrDefault();
            }
        }

        public int UpdateOutProdDetail(int id, int amount)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                ObjectParameter obj = new ObjectParameter("returnValue", typeof(int));
                dbContext.pOutProdDetail_Update(id, amount, obj);
                return (int)obj.Value;
            }
        }

        public void DeleteOutProdDetail(int detailId)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                dbContext.pOutProdDetail_Delete(detailId);
            }
        }

        public List<pOutProdDetail_Get_Result> GetOutProdDetails(int paperId)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                return dbContext.pOutProdDetail_Get(paperId).ToList();
            }
        }

        public OutProdDetail GetOutProdDetailById(int id)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                return dbContext.OutProdDetails.Find(id);
            }
        }

        public List<pOutProdHistory_Get_Result> GetOutProductHistory(int productId, DateTime? fromDate = null, DateTime? toDate = null)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                var histories = dbContext.pOutProdHistory_Get(productId, fromDate, toDate).ToList();
                return histories;
            }
        }
        #endregion Detail
    }
}
